<?php
/*
 * File name:restaurent.php
 * Create by DucNN
 * Create date: 29/10/2012
 */
class Restaurents extends MX_Controller
{   
    function __construct() {
        parent::__construct();                                          
    }
    
    function index($keyword = '~', $row = 0)
    {
        $res = new Restaurent();
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }
        
        //paging
        include('paging.php');        		
        $config['base_url']= base_url()."/administrator/restaurents/index/".$data['key_word'].'/';         
        
        if($data['key_word']!='~')
        {
            $res->like('name', $data['key_word']);
        }
        
        $config['total_rows'] = $res->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $res = new Restaurent();
        $res->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $res->like('name', $data['key_word']);
        }
                       
        $data['list_res'] = $res->get();
        $data['view'] = 'restaurents_index';
        $this->load->view('back_end/template_noright',$data);
    }
    function add()
    {
        $language = new Language();
        $language->where('status','enable')->get();
        $flag = FALSE;
        foreach ($language as $lang)
        {
            if($this->input->post('name_'.$lang->code))
            {
                $slug =$this->common->get_link($this->input->post('name_'.$lang->code),'Restaurent');
                $flag = TRUE;
            }
        }
        if ($flag)
        {
            $restaurent = new Restaurent();
            
            $restaurentmeta_title = new Restaurentmeta();
            $restaurentmeta_title->meta_key = 'seo_title';
            
            $restaurentmeta_des = new Restaurentmeta();
            $restaurentmeta_des->meta_key = 'seo_description';
            
            $restaurentmeta_key = new Restaurentmeta();
            $restaurentmeta_key->meta_key = 'seo_keyword';
             
            foreach ($language as $lang)
            {
                if($this->input->post('name_'.$lang->code))
                {            
                    $name = $this->input->post('name_'.$lang->code);
                    //echo $name;
                    $phone = $this->input->post('phone_'.$lang->code);      
                    $fax = $this->input->post('fax_'.$lang->code);		
                    $email = $this->input->post('email_'.$lang->code);    
                    $address = $this->input->post('address_'.$lang->code);      
                    $description = $this->input->post('description_'.$lang->code);		
                    $content = $this->input->post('content_'.$lang->code);      
                    $status = $this->input->post('status');		
                    $image = $this->input->post('image');     
                    $create_date = $this->input->post('create_date');
                               
                    $seo_title = $this->input->post('seo_title_'.$lang->code);  
                    $seo_description = $this->input->post('seo_description_'.$lang->code);
                    $seo_keyword = $this->input->post('seo_keyword_'.$lang->code);
                          
                    
                             
                                                                        
                    $prefix = '<!--:'.$lang->code.'-->';
                    $suffix = '<!--:-->';
                    $restaurent->name .= $prefix.$name.$suffix;
                    $restaurent->phone .= $prefix.$phone.$suffix;
                    $restaurent->fax .= $prefix.$fax.$suffix;
                    $restaurent->email .= $prefix.$email.$suffix;
                    $restaurent->address .= $prefix.$address.$suffix;
                    $restaurent->description .= $prefix.$description.$suffix;
                    $restaurent->content .= $prefix.$content.$suffix;
                    
                    //$restaurent->slug = $slug;
                                                                         
                    //add seo title                
                    $restaurentmeta_title->meta_value .= $prefix.$seo_title.$suffix;                        
                    //add seo desciption                
                    $restaurentmeta_des->meta_value .= $prefix.$seo_description.$suffix;                        
                    //add seo keywords                
                    $restaurentmeta_key->meta_value .= $prefix.$seo_keyword.$suffix;                                                                                                           
                        
                }                          
            }
            if($create_date)
            {
                $format = 'd-m-Y H:i:s';
                $create_date = DateTime::createFromFormat($format, $create_date);
                $create_date = $create_date->format('Y-m-d H:i:s');                
            }
            else {
                $create_date = date('Y-m-d H:i:s');
            } 
            $restaurent->status = $status;
            $restaurent->image = $image;
            $restaurent->create_date = $create_date;
            $restaurent->slug = $slug;
            if($restaurent->save())
            {
                $restaurentmeta_title->save($restaurent);
                $restaurentmeta_des->save($restaurent);
                $restaurentmeta_key->save($restaurent);
            } 
           redirect('administrator/restaurents');
        } else
        {
            $language = new Language();
            $language->where('status','enable')->get();
            $data['list_lang'] = $language;
            $data['view'] = 'restaurents_add';
            $this->load->view('back_end/template_noright',$data);
        }
    }
    function edit($id = 0)
    {
         if($this->input->post('name'))
        {            
            $name = $this->input->post('name');
            //echo $name;
            $phone = $this->input->post('phone');      
            $fax = $this->input->post('fax');		
            $email = $this->input->post('email');    
            $address = $this->input->post('address');      
            $description = $this->input->post('description');		
            $content = $this->input->post('content');      
            $status = $this->input->post('status');		
            $image = $this->input->post('image');     
            $create_date = $this->input->post('create_date');
                       
            $seo_title = $this->input->post('seo_title');  
            $seo_description = $this->input->post('seo_description');
            $seo_keyword = $this->input->post('seo_keyword');
                  
            
            if($create_date)
            {
                $format = 'd-m-Y H:i:s';
                $create_date = DateTime::createFromFormat($format, $create_date);
                $create_date = $create_date->format('Y-m-d H:i:s');                
            }
            else {
                $create_date = date('Y-m-d H:i:s');
            }            
            
            $slug = $this->input->post('slug');
            $slug =$this->common->get_link($slug,'Restaurent');
        
        
            $restaurent = new Restaurent();
            $restaurent->get_by_id($id);
            $restaurent->name = $name;
            $restaurent->phone = $phone;
            $restaurent->fax = $fax;
            $restaurent->email = $email;
            $restaurent->address = $address;
            $restaurent->description = $description;
            $restaurent->content = $content;
            $restaurent->status = $status;
            $restaurent->image = $image;
            $restaurent->create_date = $create_date;
            $restaurent->slug = $slug;
             
            if($restaurent->save())
            {
                
                //add seo title
                $restaurentmeta = new Restaurentmeta();
                $restaurentmeta->where(array('restaurent_id'=>$id,'meta_key'=>'seo_title'))->update('meta_value',$seo_title);
                
        
                //add seo desciption
                $restaurentmeta = new Restaurentmeta();
                $restaurentmeta->where(array('restaurent_id'=>$id,'meta_key'=>'seo_description'))->update('meta_value',$seo_description);
                        
                //add seo keywords
                $restaurentmeta = new Restaurentmeta();
                $restaurentmeta->where(array('restaurent_id'=>$id,'meta_key'=>'seo_keyword'))->update('meta_value',$seo_keyword);                                                     
            }
           redirect('administrator/restaurents');
                
        } else
        {
            $res = new Restaurent();
            $data['restaurent'] = $res->get_by_id($id);
            $data['seo_title'] = $res->get_restaurentmeta($id, 'seo_title');
            $data['seo_description'] = $res->get_restaurentmeta($id,'seo_description');
            $data['seo_keyword'] = $res->get_restaurentmeta($id,'seo_keyword');
            $data['view'] = 'restaurents_edit';
            $data['id'] = $id;
            $this->load->view('back_end/template_noright',$data);
        }
    }
    function delete()
    {
        $id = $this->input->post('param');
        
        $res = new Restaurent();
        $res->get_by_id($id);
        
        $restaurentmeta = new Restaurentmeta();

        $restaurentmeta->where('id',$id)->get();        
        $restaurentmeta->where('restaurent_id',$id)->get();

        $restaurentmeta->delete_all($res);
        
        $res->delete();
        
    }        	
}
?>
